package android.test;

import java.util.Timer;

import com.hh.fudan.project_management.R;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class NotificationActivity extends Activity implements OnClickListener{
	
	Button btest;
	
	Button bsetting;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btest = (Button) findViewById(R.id.test);
        btest.setOnClickListener(this);
        
        bsetting = (Button) findViewById(R.id.prefs);
        bsetting.setOnClickListener(this);
        
        SharedPreferences prefs = getSharedPreferences("usersettings", 0);
        PreferencesHandler prefer = PreferencesHandler.INSTANCE;
        String cityName = prefs.getString("cityName", "");
        String title = prefs.getString("title", "");
        String price = prefs.getString("price", "");
        String discountPercent = prefs.getString("discountPercent", "");
        String members = prefs.getString("members", "0");
        prefer.setCityName(cityName);
        prefer.setTitle(title);
        prefer.setMembers(Integer.parseInt(members));
        if (price != null && price.length() > 0) prefer.setPrice(Double.parseDouble(price));
        if (discountPercent != null && discountPercent.length() > 0) prefer.setDiscountPercent(Double.parseDouble(discountPercent));
       
    }
    
    public void start(){
    	Timer timer = new Timer();
        //timer.scheduleAtFixedRate(new NotificationSpider(this),0,100000);
    }
    
    public void addNotification(NotificationResult result){
    	String ns = Context.NOTIFICATION_SERVICE;
    	NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
    	
    	int icon = R.drawable.icon; 
    	CharSequence tickerText = "您有" + result.getNum() + "条新团购"; 
    	long when = System.currentTimeMillis(); 
    	Notification notification = new Notification(icon,tickerText,when);
    	
    	Context context = getApplicationContext(); 
    	CharSequence contentTitle = "您有" + result.getNum() + "条新团购，其中" + result.getNearbyNum() + "条在您的位置附近"; //֪ͨ8����
    	CharSequence contentText = result.getContents(); 

    	Intent notificationIntent = new Intent();
    	PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
    	notification.contentIntent = contentIntent;
    	notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    	mNotificationManager.notify(0,notification);

    }
    
    public void onClick(View v) {
		//System.out.println(v.getId());
		if (v.getId() == btest.getId()) start();
		
		/*if (v.getId() == bsetting.getId()) {
			 Intent intent = new Intent();
			 intent.setClass(NotificationActivity.this, PreferencesSettingsActivity.class);
			 startActivity(intent);
		}*/
    }
    
}